﻿/******************************************************************************
* File description: BoxFailureInfo_plus.qml
* Function: 实验故障弹窗2.0
* Current Version: 1.0.0
* Author: JNN
* Date: 2025-10-20
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/

import QtQuick 2.0
import "../components"
CustomPopupBox {
    id: root
    title: "故障"

    property var failureInfoList: []            // [["故障信息", "并发"], ["故障信息", "主流程"]]

    property int showContinueButton: 0          // 0可继续 1不可继续 2非实验状态 3断网故障（可继续，已取消）

    showCloseButton: true
    height: lineHeight * 12

    property int lineHeight: 35*dp
    property int marginWidth: 16*dp
    property bool hasProcessFailure: false
    property bool hasUnionFailure: false

    isMovable: true

    function clearFailureInfo_process(){
        let tmpList = failureInfoList
        failureInfoList = []
        for(let i = 0; i < tmpList.length; i ++){
            if(tmpList[i][1] === "并发"){
                failureInfoList.push(tmpList[i])
            }else if(tmpList[i][1] === "流程+并发"){
                failureInfoList.push([tmpList[i][0], "并发"])
            }
        }
        failureInfoListChanged()
    }
    function clearFailureInfo_union(){
        let tmpList = failureInfoList
        failureInfoList = []
        for(let i = 0; i < tmpList.length; i ++){
            if(tmpList[i][1] === "流程"){
                failureInfoList.push(tmpList[i])
            }else if(tmpList[i][1] === "流程+并发"){
                failureInfoList.push([tmpList[i][0], "流程"])
            }
        }
        failureInfoListChanged()
    }


    property string currentStep: "当前步骤-无"
    onVisibleChanged: {
        if(gExpIsRunning === 1){
            currentStep = CGetMessageInterface.getCurrentStepInfo()
        }else{
            currentStep = ""
        }

        edit_step.setText("")
    }

    function isYiChang(text){
        if(text==="设备连接通信异常"){
            return !gConnectStatus
        }else{
            return true
        }
    }

    function updateHasFailureStatus(){
        hasProcessFailure = false
        hasUnionFailure = false
        for(let i = 0; i < failureInfoList.length; i ++){
            if(failureInfoList[i][1] === "流程"){
                hasProcessFailure = true
            }else if(failureInfoList[i][1] === "并发"){
                hasUnionFailure = true
            }else if(failureInfoList[i][1] === "流程+并发"){
                hasUnionFailure = true
                hasProcessFailure = true
            }
        }
        // 只有<并发>或<流程>故障其中一种时，增加判断
        if((hasProcessFailure) && (!hasUnionFailure)){
            radioBox_union.checkedIndex = 0
        }else if((!hasProcessFailure) && (hasUnionFailure)){
            radioBox_union.checkedIndex = 1
        }
    }

    onFailureInfoListChanged: {
        updateHasFailureStatus()
    }

    property var tableScale: [0.08, 0.66, 0.14, 0.12]

    // 表格
    Rectangle{
        id: layout_top
        width: parent.width - 2*marginWidth
        height: lineHeight * 6
        x: marginWidth
        y: marginWidth
        // 表头
        Rectangle{
            height: lineHeight; width: parent.width
            Rectangle{ color: "#CCC"; height: 1;  width: parent.width; y: 0 }
            Row{
                anchors.fill: parent
                Rectangle{ height: parent.height; width: 1; color: "#CCC" }
                Item{
                    width: parent.width * tableScale[0] - 1
                    height: parent.height
                    Text {
                        text: qsTr("序号")
                        anchors.centerIn: parent
                    }
                }
                Rectangle{ height: parent.height; width: 1; color: "#CCC" }
                Item{
                    width: parent.width * tableScale[1] - 1
                    height: parent.height
                    Text {
                        text: qsTr("故障信息")
                        anchors.centerIn: parent
                    }
                }
                Rectangle{ height: parent.height; width: 1; color: "#CCC" }
                Item{
                    width: parent.width * tableScale[2] - 1
                    height: parent.height
                    Text {
                        text: qsTr("故障类型")
                        anchors.centerIn: parent
                    }
                }
                Rectangle{ height: parent.height; width: 1; color: "#CCC" }
                Item{
                    width: parent.width * tableScale[3] - 2
                    height: parent.height
                    Text {
                        text: qsTr("故障状态")
                        anchors.centerIn: parent
                    }
                }
                Rectangle{ height: parent.height; width: 1; color: "#CCC" }
            }
            Rectangle{ color: "#CCC"; height: 1;  width: parent.width; y: parent.height - height }
        }
        ListView{
            y: lineHeight
            clip: true
            height: 5*lineHeight
            width: parent.width
            model: Math.max(failureInfoList.length, 5)
            delegate: Rectangle{
                height: lineHeight
                width: parent.width
                color: {
                    if(index < failureInfoList.length){
                        if(isYiChang(failureInfoList[index][0])){
                            return "#FEF5F5"        // 浅红
                        }else{
                            return "#F7FEF4"        // 浅绿
                        }
                    }
                    return "white"
                }
                Row{
                    anchors.fill: parent
                    Rectangle{ height: parent.height; width: 1; color: "#CCC" }
                    Item{
                        width: parent.width * tableScale[0] - 1
                        height: parent.height
                        Text {      // 序号
                            text: index < failureInfoList.length ? (index+1) : "--"
                            anchors.centerIn: parent
                        }
                    }
                    Rectangle{ height: parent.height; width: 1; color: "#CCC" }
                    Item{
                        width: parent.width * tableScale[1] - 1
                        height: parent.height
                        Text {      // 异常信息内容
                            text: index < failureInfoList.length ? failureInfoList[index][0] : "--"
                            anchors.centerIn: parent
                        }
                    }
                    Rectangle{ height: parent.height; width: 1; color: "#CCC" }
                    Item{
                        width: parent.width * tableScale[2] - 1
                        height: parent.height
                        Text {      // 故障属于<流程>还是<并发>
                            text: index < failureInfoList.length ? failureInfoList[index][1] : "--"
                            anchors.centerIn: parent
                        }
                    }
                    Rectangle{ height: parent.height; width: 1; color: "#CCC" }
                    Item{
                        width: parent.width * tableScale[3] - 2
                        height: parent.height
                        Text {      // 正常/异常
                            text: index < failureInfoList.length ? (isYiChang(failureInfoList[index][0])?"异常":"正常") : "--"
                            anchors.centerIn: parent
                            color: {

                                if(index < failureInfoList.length){
                                    if(text === "正常"){
                                        return "#3A4"
                                    }else if(text === "异常"){
                                        return "#D43"
                                    }
                                }
                                return "black"
                            }
                        }
                    }
                    Rectangle{ height: parent.height; width: 1; color: "#CCC" }
                }
                Rectangle{ color: "#CCC"; height: 1;  width: parent.width; y: parent.height - height }
            }
        }

    }
    // 表格下面的一行小字
    Column{
        x: marginWidth + 3*dp
        y: layout_top.y + layout_top.height + 3*dp
        Text {
            text: (gExpIsRunning === 1) ? currentStep : ""
            font.pixelSize: config.fontsize
        }
        Text {
            text: "当前故障无法继续进行实验"
            font.pixelSize: config.fontsize
            visible: showContinueButton === 1
        }
        CustomRadioGroup{
            id: radioBox_union
            titleWidth: 100*dp
            title: "处理故障类型"
//            editable: hasUnionFailure && hasProcessFailure
            visible: showContinueButton === 0 && gExpIsFailure > 0 && (hasUnionFailure && hasProcessFailure)
            model: ["流程", "并发"]
        }
//        CustomCheckBox{
//            id: checkBox_union
//            title: "是否执行并发"
//            visible: showContinueButton === 0 && gStateExp.isUnion === true
//        }
    }


    // 下面的按钮
    Item{
        id: layout_bottom
        width: parent.width - 2*marginWidth
        height: 40*dp
        x: marginWidth
        y: parent.height - height - marginWidth
        Row{
            anchors.centerIn: parent
            spacing: 6*dp
            // 按钮1 ===============================================
            CustomButton{
                title: "停止实验"
                defaultMode2: true
                enabled: gExpIsRunning === 1 && gExpIsFailure > 0
                onClicked: {
                    box_confirm.tempData = 1
                    box_confirm.show()
                }
            }
            // 按钮2 ================================================
            CustomButton{
                title: "重新连接"
                defaultMode2: true
                enabled: gConnectStatus === false
                onClicked: {
                    CGetMessageInterface.reconnect()
                    timer_reconnect.start()
                }
            }
            // 重新连接如果没反应给个提示
            Timer{
                id: timer_reconnect
                interval: 2000
                repeat: false
                onTriggered: {
                    if(gConnectStatus === false){
                        toast.show("连接失败", 1)
                    }
                }
            }
            // 按钮3 ================================================
            Rectangle{
                width: 160*dp
                height: 40*dp
                border.color: "#CCC"
                radius: 3*dp
                Item{
                    height: 40*dp; width: 120*dp
                    Row{
                        anchors.centerIn: parent
                        spacing: 1*dp
                        Text{
                            text: "执行第"
                            font.pixelSize: config.fontsize
                            anchors.verticalCenter: parent.verticalCenter
                        }
                        CustomInputText{
                            id: edit_step
                            intflag: true
                            editHeight: 30*dp; editWidth: 45*dp
                            anchors.verticalCenter: parent.verticalCenter
                            inputMaxLength: 5
                        }
                        Text{
                            text: "步"
                            anchors.verticalCenter: parent.verticalCenter
                        }
                    }
                }
                Rectangle{ x: 119*dp; height: parent.height; width: 1; color: "#CCC" }
                CustomButton{
                    x: 120*dp
                    height: 38*dp; width: 39*dp
                    button_icon: "qrc:/picture/controlPannelButton/run.png"
                    anchors.verticalCenter: parent.verticalCenter
                    onClicked: {
                        if(Number(edit_step.getText())<=0 || Number(edit_step.getText()) > 1000000){
                            toast.show("请输入正确的步骤", 1)
                            return
                        }
                        box_confirm.tempData = 3
                        box_confirm.show()
                    }
                }
                Rectangle{      // 禁用遮罩
                    anchors.fill: parent
                    visible: !(gExpIsRunning === 1 && gExpIsFailure > 0 && showContinueButton === 0  && gConnectStatus && hasProcessFailure && radioBox_union.checkedIndex===0)
                    radius: 3*dp
                    color: "#77EEEEEE"
                    MouseArea{
                        anchors.fill: parent
                    }
                }
            }
            // 按钮4 ==================================================================
            CustomButton{
                title: "重新执行"
                defaultMode2: true
                enabled: gExpIsRunning === 1 && gExpIsFailure > 0 && showContinueButton === 0 && gConnectStatus
                onClicked: {
                    box_confirm.tempData = 4
                    box_confirm.show()
                }
            }
            // 按钮5 ==================================================================
            CustomButton{
                title: "继续下一步"
                defaultMode2: true
                enabled: gExpIsRunning === 1 && gExpIsFailure > 0 && showContinueButton === 0 && gConnectStatus
                onClicked: {
                    box_confirm.tempData = 5
                    box_confirm.show()
                }
            }
        }
    }

    CustomPopupBoxWarning{
        id: box_confirm
        tempData: 1
        box_text: {
            if(tempData === 1)      return "确认停止实验吗?"
            if(tempData === 2)      return "确认重新连接吗?"
            if(tempData === 3)      return "确认从步骤【" + edit_step.getText() + "】开始重新执行吗?"
            if(tempData === 4)      return "确认重新执行当前步骤吗?"
            if(tempData === 5)      return "确认执行下一步吗?"
        }
        onAccepted: {
            if(tempData === 1){
                if(gConnectStatus){     // 连接状态时，发送停止信号
                    box_opEndExp.startWait01()
                    box_opEndExp.show()
                    close()
                    failureInfoList = []
                    showContinueButton = 0
                    CommunicationMethod.operationExperiment(2)
                }else{                  // 非连接状态时，直接终止
                    close()
                    CommunicationMethod.forcedStop()
                }
            }else if(tempData === 2){

            }else if(tempData === 3){
                let tmp = gExpIsFailurePause
                gExpIsFailurePause = 0
                let res = CommunicationMethod.errorContinueExp(2, Number(edit_step.getText()), radioBox_union.checkedIndex)
                if(res){
                    // 清除故障信息
                    if(radioBox_union.checkedIndex === 0){
                        clearFailureInfo_process()
                        gExpIsFailure = gExpIsFailure & 2
                    }else{
                        clearFailureInfo_union()
                        gExpIsFailure = gExpIsFailure & 1
                    }
                    root.close()
                }else{
                    gExpIsFailurePause = tmp
                    toast.show("请输入正确的步骤", 1)
                }
            }else if(tempData === 4){
                gExpIsFailurePause = 0
                CommunicationMethod.errorContinueExp(0, 0, radioBox_union.checkedIndex)
                // 清除故障信息
                if(radioBox_union.checkedIndex === 0){
                    clearFailureInfo_process()
                    gExpIsFailure = gExpIsFailure & 2
                }else{
                    clearFailureInfo_union()
                    gExpIsFailure = gExpIsFailure & 1
                }
                root.close()
            }else if(tempData === 5){
                gExpIsFailurePause = 0
                CommunicationMethod.errorContinueExp(1, 0, radioBox_union.checkedIndex)
                // 清除故障信息
                if(radioBox_union.checkedIndex === 0){
                    clearFailureInfo_process()
                    gExpIsFailure = gExpIsFailure & 2
                }else{
                    clearFailureInfo_union()
                    gExpIsFailure = gExpIsFailure & 1
                }
                root.close()
            }
        }
    }

}
